package com.ruoyi.project.yuwen.yuwenfeedback.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.yuwen.audioblack.domain.AudioBlack;
import com.ruoyi.project.yuwen.audioblack.mapper.AudioBlackMapper;
import com.ruoyi.project.yuwen.feedbackstat.domain.FeedbackStat;
import com.ruoyi.project.yuwen.feedbackstat.mapper.FeedbackStatMapper;
import com.ruoyi.project.yuwen.yuwenaudio.domain.Audio;
import com.ruoyi.project.yuwen.yuwenaudio.mapper.AudioMapper;
import com.ruoyi.project.yuwen.yuwenfeedback.domain.AudioFeedback;
import com.ruoyi.project.yuwen.yuwenfeedback.mapper.AudioFeedbackMapper;
import com.ruoyi.project.yuwen.yuwenfeedback.service.IAudioFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 朗读举报Service业务层处理
 *
 * @author shixiangwen
 * @date 2021-02-24
 */
@Service
@DataSource(DataSourceType.KKCUSER)
public class AudioFeedbackServiceImpl implements IAudioFeedbackService {

    @Autowired
    private AudioFeedbackMapper audioFeedbackMapper;

    @Autowired
    private AudioMapper audioMapper;

    @Autowired
    private AudioBlackMapper audioBlackMapper;

    @Autowired
    private FeedbackStatMapper feedbackStatMapper;

    /**
     * 查询朗读举报
     *
     * @param id 朗读举报ID
     * @return 朗读举报
     */
    @Override
    public AudioFeedback selectAudioFeedbackById(Long id) {
        return audioFeedbackMapper.selectAudioFeedbackById(id);
    }

    /**
     * 查询朗读举报列表
     *
     * @param audioFeedback 朗读举报
     * @return 朗读举报
     */
    @Override
    public List<AudioFeedback> selectAudioFeedbackList(AudioFeedback audioFeedback) {
        List<AudioFeedback> feedbacks = audioFeedbackMapper.selectAudioFeedbackList(audioFeedback);
        Map<Integer, FeedbackStat> cache = new HashMap<>();
        feedbacks.forEach(item -> {
            FeedbackStat stat;
            if (cache.containsKey(item.getReporter())) {
                stat = cache.get(item.getReporter());
            } else {
                stat = feedbackStatMapper.selectFeedbackStatById(item.getReporter());
                cache.put(item.getReporter().intValue(), stat);
            }
            if (stat != null) {
                item.setUserStat(stat.getPass() + "/" + (stat.getNotPass() + stat.getPass()));
            } else {
                item.setUserStat("0/0");
            }
            //Audio audio = audioMapper.selectAudioById(item.getVid());
            //if (audio != null) {
            //    item.setUrl(audio.getUrl());
            //}
        });
        return feedbacks;
    }

    /**
     * 新增朗读举报
     *
     * @param audioFeedback 朗读举报
     * @return 结果
     */
    @Override
    public int insertAudioFeedback(AudioFeedback audioFeedback) {
        return audioFeedbackMapper.insertAudioFeedback(audioFeedback);
    }

    /**
     * 修改朗读举报
     *
     * @param audioFeedback 朗读举报
     * @return 结果
     */
    @Override
    public int updateAudioFeedback(AudioFeedback audioFeedback) {
        return audioFeedbackMapper.updateAudioFeedback(audioFeedback);
    }

    /**
     * 删除朗读举报对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAudioFeedbackByIds(String ids) {
        return audioFeedbackMapper.deleteAudioFeedbackByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除朗读举报信息
     *
     * @param id 朗读举报ID
     * @return 结果
     */
    @Override
    public int deleteAudioFeedbackById(Long id) {
        return audioFeedbackMapper.deleteAudioFeedbackById(id);
    }

    @Override
    public int approve(Long id, int approveResult) {

        AudioFeedback feedback = audioFeedbackMapper.selectAudioFeedbackById(id);
        final boolean pass = approveResult == 3 ? false : true;

        if (approveResult == 1 || approveResult == 2) {
            Audio audio = audioMapper.selectAudioById(feedback.getVid());
            //已经是下架的就不处理了
            if (audio.getVstatus() != -4) {
                audio.setVstatus(approveResult == 2 ? -4 : 1);
            }
            audioMapper.updateAudio(audio);
            //封禁用户
            if (approveResult == 2) {
                AudioBlack query = new AudioBlack();
                query.setUid(audio.getKkuid());
                List<AudioBlack> blacks = audioBlackMapper.selectAudioBlackList(query);
                if (blacks.isEmpty()) {
                    AudioBlack black = new AudioBlack();
                    black.setUid(audio.getKkuid());
                    black.setCreatedTime(DateUtils.getNowDate());
                    black.setBlockEndtime(DateUtils.addDays(DateUtils.getNowDate(), 360));
                    black.setCreateUserId(ShiroUtils.getLoginName());
                    audioBlackMapper.insertAudioBlack(black);
                } else {
                    AudioBlack black = blacks.get(0);
                    black.setBlockEndtime(DateUtils.addDays(DateUtils.getNowDate(), 360));
                    audioBlackMapper.updateAudioBlack(black);
                }
            }
        }

        //同一个音频的多个举报一起处理
        AudioFeedback query = new AudioFeedback();
        query.setVid(feedback.getVid());
        query.setApproveStatus(0);
        List<AudioFeedback> feedbacks = audioFeedbackMapper.selectAudioFeedbackList(query);
        feedbacks.forEach(item -> {
            item.setApproveResult(approveResult);
            item.setApproveStatus(1);
            item.setApproveTime(DateUtils.getNowDate());
            item.setApproveUser(ShiroUtils.getLoginName());
            audioFeedbackMapper.updateAudioFeedback(item);
            FeedbackStat stat = feedbackStatMapper.selectFeedbackStatById(item.getReporter());
            if (stat == null) {
                stat = new FeedbackStat();
                stat.setCreatedTime(DateUtils.getNowDate());
                stat.setPass(pass ? 1L : 0);
                stat.setNotPass(pass ? 0L : 1L);
                stat.setUid(item.getReporter());
                feedbackStatMapper.insertFeedbackStat(stat);
            } else {
                if (pass) {
                    stat.setPass(stat.getPass() + 1);
                } else {
                    stat.setNotPass(stat.getNotPass() + 1);
                }
                feedbackStatMapper.updateFeedbackStat(stat);
            }
        });

        return 1;
    }
}
